package net.confinanz.client.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import net.confinanz.client.controller.Controller;
import net.confinanz.client.exceptions.NotConnectedException;

public class Dokument {

	private static final String SELECT 		= "SELECT * FROM `ent_dokument` ";
	private static final String INSERT 		= "INSERT INTO `confinanz`.`ent_dokument` (`dokument_name`, `dokument_typ`) VALUES (";
	private static final String UPDATE 		= "UPDATE `ent_dokument` SET ";
	
	private String id;
	private String name;
	private Dokumenttyp typ;

	public Dokument() {
		this.setId("");
		this.setName("");
		this.setTyp(new Dokumenttyp());
	}
	
	public Dokument(String id, Controller controller) throws SQLException, NotConnectedException {
		setId(id);
		fromDatabase(controller);
	}
	

//	##### Instanz Methoden #####

	public String getId() {
		return id;
	}

	private void setId(String id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Dokumenttyp getTyp() {
		return typ;
	}

	public void setTyp(Dokumenttyp typ) {
		this.typ = typ;
	}

	public String toStringExtended() {
		return (this.id + " : " + this.name + " : " + this.typ.getBezeichner());
	}
	
	public String toStringDatabase() {
		return (this.id + " : " + this.name + " : " + this.typ.getId());
	}
	
	
//	##### Datenbank Methoden #####
	
	private void fromDatabase(Controller controller) throws SQLException, NotConnectedException {
		String sql = SELECT + "WHERE `dokument_id` = " + getId();
		controller.logMessage("SQL-QUERY: " + sql);
		ResultSet result = controller.getConnector().select(sql);
		
		if (result.first()) {
			setName(result.getString("dokument_adresse"));
			setTyp(new Dokumenttyp(result.getString("dokument_typ"), controller));
			
			controller.logMessage("RESULT: " + toStringDatabase());
		} else throw new SQLException("There is no dataset with `dokument_id` = " + getId() + " in table `ent_dokument`.");
	}
	
	public void toDatabase(Controller controller) throws SQLException, NotConnectedException {
		String sql;
		if (getId().equals("")) {
			sql = INSERT + "'" + getName()
					+ "', '" + getTyp().getId()
					+ "')";
			
			controller.logMessage("SQL-QUERY: " + sql);
			controller.getConnector().insert(sql);

			sql = Controller.INSERT_ID;
			controller.logMessage("SQL-QUERY: " + sql);
			ResultSet result = controller.getConnector().select(sql);
			
			if (result.first()) {
				setId(result.getString("id"));
			}
			controller.logMessage("RESULT: " + toStringDatabase());
		} else {
			sql = UPDATE + "`dokument_name` = '" + getName()
					+ "`dokument_typ` = '" + getTyp().getId()
					+ "' WHERE `dokument_id` = " + getId();
			
			controller.logMessage("SQL-QUERY: " + sql);
			controller.getConnector().insert(sql);
			controller.logMessage("RESULT: " + toStringDatabase());
		}
	}
	
	
//	##### Override Methoden #####
	
	@Override
	public String toString() {
		return this.typ.getBezeichner() + ": " + this.name;
	}
	
	@Override
	public boolean equals(Object object) {
		return ((object instanceof Dokument) && (((Dokument) object)).getId().equals(this.getId()));
	}
}